package com.ruoyi.project.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.project.system.domain.SysRoleDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.CityTypeMapper;
import com.ruoyi.project.system.domain.CityType;
import com.ruoyi.project.system.service.ICityTypeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 城市分类关联Service业务层处理
 * 
 * @author jjp
 * @date 2020-04-09
 */
@Service
public class CityTypeServiceImpl implements ICityTypeService 
{
    @Autowired
    private CityTypeMapper cityTypeMapper;

    /**
     * 查询城市分类关联
     * 
     * @param typeId 城市分类关联ID
     * @return 城市分类关联
     */
    @Override
    public CityType selectCityTypeById(Long typeId)
    {
        return cityTypeMapper.selectCityTypeById(typeId);
    }

    /**
     * 查询城市分类关联列表
     * 
     * @param cityType 城市分类关联
     * @return 城市分类关联
     */
    @Override
    public List<CityType> selectCityTypeList(CityType cityType)
    {
        return cityTypeMapper.selectCityTypeList(cityType);
    }

    /**
     * 新增城市分类关联
     * 
     * @param cityType 城市分类关联
     * @return 结果
     */
    @Override
    public int insertCityType(CityType cityType)
    {
        return cityTypeMapper.insertCityType(cityType);
    }

    /**
     * 修改城市分类关联
     * 
     * @param cityType 城市分类关联
     * @return 结果
     */
    @Override
    public int updateCityType(CityType cityType)
    {
        return cityTypeMapper.updateCityType(cityType);
    }

    /**
     * 批量删除城市分类关联
     * 
     * @param typeIds 需要删除的城市分类关联ID
     * @return 结果
     */
    @Override
    public int deleteCityTypeByIds(Long[] typeIds)
    {
        return cityTypeMapper.deleteCityTypeByIds(typeIds);
    }

    /**
     * 删除城市分类关联信息
     * 
     * @param typeId 城市分类关联ID
     * @return 结果
     */
    @Override
    public int deleteCityTypeById(Long typeId)
    {
        return cityTypeMapper.deleteCityTypeById(typeId);
    }

    @Override
    public int updateCityService(CityType cityType)
    {
         cityTypeMapper.deleteCityServiceById(cityType.getCityId());
         return insertCityService(cityType);
    }

    /**
     * 循环添加 城市关联的服务
     *
     * */

    public int insertCityService(CityType role){
        int rows = 1;

        List<CityType> list = new ArrayList<>();

        for (Long typeIds : role.getTypeIds())
        {
            CityType cityType = new CityType();
            cityType.setCityId(role.getCityId());
            cityType.setTypeId(typeIds);
            list.add(cityType);
        }
        if (list.size() != 0)
        {
            rows = cityTypeMapper.insertcityServiceById(list);
        }
        return rows;
    }
}
